package cn.stylefeng.guns.modular.business.frequency.agreement.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.exception.enums.StatusExceptionEnum;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.agreement.entity.Agreement;
import cn.stylefeng.guns.modular.business.frequency.agreement.enums.AgreementExceptionEnum;
import cn.stylefeng.guns.modular.business.frequency.agreement.mapper.AgreementMapper;
import cn.stylefeng.guns.modular.business.frequency.agreement.model.param.AgreementEditParam;
import cn.stylefeng.guns.modular.business.frequency.agreement.model.param.AgreementPageParam;
import cn.stylefeng.guns.modular.business.frequency.agreement.model.param.AgreementParam;
import cn.stylefeng.guns.modular.business.frequency.agreement.model.result.AgreementDetailResult;
import cn.stylefeng.guns.modular.business.frequency.agreement.model.result.AgreementPageResult;
import cn.stylefeng.guns.modular.business.frequency.agreement.service.AgreementService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 协议表 服务实现类
 *
 * @author 李文龙
 * @date 2021/10/28 13:58
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class AgreementServiceImpl extends ServiceImpl<AgreementMapper, Agreement> implements AgreementService {

    @Override
    public PageResult<AgreementPageResult> page(AgreementPageParam agreementParam) {

        Page<AgreementPageResult> page = this.baseMapper.page(PageFactory.defaultPage(), agreementParam.getName());


        // 查询分页结果
        return new PageResult<>(page);
    }

    @Override
    public List<Agreement> list(AgreementParam agreementParam) {

        // 构造条件
        LambdaQueryWrapper<Agreement> queryWrapper = new LambdaQueryWrapper<>();

        // 查询未删除的

        return this.list(queryWrapper);
    }

    @Override
    public void add(AgreementParam agreementParam) {

        // 校验参数，检查是否存在重复的编码或者名称，不排除当前添加的这条记录
        checkParam(agreementParam, false);

        // 将dto转为实体
        Agreement agreement = new Agreement();
        BeanUtil.copyProperties(agreementParam, agreement);

        // 设置状态为启用

        this.save(agreement);
    }

    @Override
    public void delete(AgreementParam agreementParam) {

        // 根据id查询实体
        Agreement agreement = this.queryAgreement(agreementParam);

        // 逻辑删除，修改状态

        // 更新实体
        this.updateById(agreement);

    }

    @Override
    public void edit(AgreementEditParam agreementParam) {

        // 根据id查询实体
        Agreement agreement = this.getById(agreementParam.getId());
        if (ObjectUtil.isEmpty(agreement)) {
            throw new ServiceException(AgreementExceptionEnum.NOT_EXISTED);
        }
        // 请求参数转化为实体
        BeanUtil.copyProperties(agreementParam, agreement);


        this.updateById(agreement);
    }

    @Override
    public AgreementDetailResult detail(Long id) {
        AgreementDetailResult agreement = this.baseMapper.detail(id);
        if (ObjectUtil.isEmpty(agreement)) {
            throw new ServiceException(AgreementExceptionEnum.NOT_EXISTED);
        }
        return agreement;
    }

    @Override
    public void changeStatus(AgreementParam agreementParam) {
        Long id = agreementParam.getId();

        // 校验状态在不在枚举值里

        // 更新枚举，更新只能更新未删除状态的
        LambdaUpdateWrapper<Agreement> updateWrapper = new LambdaUpdateWrapper<>();
        boolean update = this.update(updateWrapper);
        if (!update) {
            throw new ServiceException(StatusExceptionEnum.UPDATE_STATUS_ERROR);
        }
    }

    /**
     * 校验参数，检查是否存在重复的编码等
     *
     * @author 李文龙
     * @since 2021/10/28 13:58
     */
    private void checkParam(AgreementParam agreementParam, boolean isExcludeSelf) {
        Long id = agreementParam.getId();
        String code = agreementParam.getCode();

        // 构建带code的查询条件
        LambdaQueryWrapper<Agreement> queryWrapperByCode = new LambdaQueryWrapper<>();
        queryWrapperByCode.eq(Agreement::getCode, code);

        // 如果排除自己，则增加查询条件主键id不等于本条id
        if (isExcludeSelf) {
            queryWrapperByCode.ne(Agreement::getId, id);
        }

        // 查询重复记录的数量
        int countByCode = this.count(queryWrapperByCode);

        // 如果存在重复的记录，抛出异常，直接返回前端
        if (countByCode >= 1) {
            throw new ServiceException(AgreementExceptionEnum.CODE_REPEAT);
        }
    }

    /**
     * 获取协议表
     *
     * @author 李文龙
     * @since 2021/10/28 13:58
     */
    private Agreement queryAgreement(AgreementParam agreementParam) {
        Agreement agreement = this.getById(agreementParam.getId());
        if (ObjectUtil.isEmpty(agreement)) {
            throw new ServiceException(AgreementExceptionEnum.NOT_EXISTED);
        }
        return agreement;
    }
}